package com.modificial.shiro.controller;

import com.modificial.shiro.system.domain.SysRole;
import com.modificial.shiro.system.service.RoleService;
import com.modificial.shiro.system.vo.RoleInfo;
import com.modificial.shiro.util.JsonResult;
import com.modificial.shiro.util.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.session.Session;
import org.apache.shiro.web.util.WebUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @author modificial
 * @date 2018/8/6 0006
 * @company modificial_org
 * @description 角色控制
 */
@RestController
@RequestMapping("/role")
@Api(tags = "角色管理")
public class RoleController {
    @Autowired
    private RoleService roleService;

    @PostMapping("/addRole")
    @ApiOperation(value = "角色添加")
    public JsonResult addRole(@RequestBody RoleInfo roleInfo){
        SysRole sysRole=new SysRole();
        BeanUtils.copyProperties(roleInfo,sysRole);
        int count=roleService.addRole(sysRole);
        if(count==1){
            JsonResult jsonResult=new JsonResult(HttpStatus.OK.value(),"添加角色信息成功",count);
            return  jsonResult;
        }
        return new JsonResult(HttpStatus.OK.value(),"添加角色信息失败",count);
    }

    @PostMapping("/updateRole")
    @ApiOperation(value = "更新用户")
    public JsonResult updateRole(@RequestBody RoleInfo roleInfo)
    {
        SysRole sysRole=new SysRole();
        BeanUtils.copyProperties(roleInfo,sysRole);
        int count=roleService.updateRole(sysRole);
        if(count==1){
            return new JsonResult(HttpStatus.OK.value(),"更新用户信息成功",1);
        }
        return new JsonResult(HttpStatus.OK.value(),"更新用户信息失败",0);
    }

    @PostMapping("/disable")
    @ApiOperation(value = "禁用或启用用户")
    public  JsonResult disable(@ApiParam(value = "角色id",required = true)String roleId,
                               @ApiParam(value = "禁用或启用",required = true)Integer status){
        int count=roleService.disableRole(roleId, status);
        if(count==1){
            JsonResult jsonResult=new JsonResult(HttpStatus.OK.value(),"禁用或启用成功",1);
            return jsonResult;
        }
        return  new JsonResult(HttpStatus.OK.value(),"禁用或启用失败",0);
    }

    @PostMapping("/findRoleById")
    @ApiOperation(value = "根据id查询角色")
    public JsonResult  findRoleById(@ApiParam(value = "角色id",required = true)@RequestParam String roleId){
        SysRole sysRole=roleService.findRoleById(roleId);
        if(sysRole!=null){
            JsonResult jsonResult=new JsonResult(HttpStatus.OK.value(),"查询角色信息成功",1);
            jsonResult.setData(jsonResult);
            return jsonResult;
        }
        return new JsonResult(HttpStatus.OK.value(),"查询角色信息失败",0);
    }

    @PostMapping("/findByPage")
    @ApiOperation(value = "分页查询角色")
    @RequiresPermissions(value = "sys-user-select")
    public JsonResult findByPage(@ApiParam(value = "开始页数")@RequestParam(required = false) Integer nowPage,
                                 @ApiParam(value = "每页显示多少条")@RequestParam(required = false) Integer pageSize,HttpServletRequest request){
        if(nowPage==null||nowPage<1){
            nowPage=1;
        }
        if(pageSize==null||pageSize<1){
            pageSize=10;
        }
       Page<SysRole>page= roleService.listRoles(nowPage, pageSize);
        if(page!=null){
            JsonResult jsonResult=new JsonResult(HttpStatus.OK.value(),"查询角色信息成功",1);
            jsonResult.setData(page);
            request.getSession(true).setAttribute("s","8");
            return jsonResult;
        }
        return new JsonResult(HttpStatus.OK.value(),"查询角色信息失败",0);
    }

    @PostMapping("/deleteRole")
    @ApiOperation(value = "删除角色信息",notes = "版本号：1.0.0")
    public JsonResult deleteRole(@ApiParam(value = "角色id",required = true)@RequestParam String roleId){
        int count=roleService.deleteRole(roleId);
        if(count>0){
            return new JsonResult(HttpStatus.OK.value(),"删除角色信息成功",1);
        }
        return new JsonResult(HttpStatus.OK.value(),"删除角色信息失败",0);
    }

    @PostMapping("/grantForRole")
    @ApiOperation(value = "为角色授权")
    public JsonResult  grantForRole(@ApiParam(value = "角色id",required = true) @RequestParam  String roleId,
                                    @ApiParam(value = "权限id集合",required = true)@RequestParam String permissions){
      int count= roleService.grantForRole(roleId, permissions);
      if(count>0){
          return new JsonResult(HttpStatus.OK.value(),"角色授权成功",1);
      }
      return new JsonResult(HttpStatus.OK.value(),"角色授权失败",0);
    }
}
